/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.service;

import com.iti.jets.gpts.home.CommentsHome;
import com.iti.jets.gpts.home.FeedbackHome;
import com.iti.jets.gpts.home.TranServiceHome;
import com.iti.jets.gpts.home.UserHome;
import com.iti.jets.gpts.pojo.Comments;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author U
 */
@Path("/comment")
public class CommentsService {

    TranServiceHome daotranservice = new TranServiceHome(util.HibernateUtil.getSessionFactory());
    UserHome daouser = new UserHome(util.HibernateUtil.getSessionFactory());
    FeedbackHome daofeedback = new FeedbackHome(util.HibernateUtil.getSessionFactory());
    CommentsHome daocomment = new CommentsHome(util.HibernateUtil.getSessionFactory());
    List result;

    @GET
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/comments/{id}")
    public JSONArray getComments(@PathParam(value = "id") int id) throws JSONException {
        JSONArray jsonCommentArray = new JSONArray();
        result = new ArrayList(daocomment.getAllComments(id));
        for (Iterator it = result.iterator(); it.hasNext();) {
            Comments comments = (Comments) it.next();
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("comment", comments.getComment());
            jSONObject.put("id", comments.getId());
//            jSONObject.put("transervice", comments.getTranService());
//            jSONObject.put("unit", comments.getUnitIdentifier());
//            jSONObject.put("user", comments.getUser());
            jsonCommentArray.put(jSONObject);
        }
        return jsonCommentArray;
    }
    @GET
    @Consumes(MediaType.TEXT_PLAIN)
     @Produces(MediaType.TEXT_PLAIN)
    @Path("/post/{id}/{comment}/{email}")
    public String PostComment(@PathParam(value = "id") int id, @PathParam(value = "comment") String comment, @PathParam(value = "email") String email) {
        Comments comm = new Comments();
        int length=daocomment.getAllComments();
        System.out.println("length------------------------------------" + length);
        comm.setId(length + 1);
        comm.setComment(comment);
        comm.setTranService(daotranservice.getObjectByID(id));
        comm.setUser(daouser.getObject(email));
        daocomment.create(comm);
        return "Done";
    }
}
